
//Chon tat ca cac checkbox
function checka(form){
		form=document.getElementById(form);
		for (var i = 0; i < form.elements.length; i++) {
			if ( form.all.checked==true ){
				form.elements[i].checked = true;
			}
			else{
				form.elements[i].checked = false;
			}
		}
}

/*
 * ham dung de goi ajax, co tich hop fix back ajax
 * div: id cua the div dung de load noi dung ajax tra ve
 * style: kieu cua ajax dung de check va confirm, co 4 kieu:
 * 			+ one: dung de confirm khi goi ajax
 * 			+ multi: check cac element trong checkbox va confirm
 * 			+ multi_nonConfirm: check cac element trong checkbox va ko confirm
 * 			+ default: mac dinh chi goi ajax, ko confirm, ko check
 * form: ten id cua form khi can sumbit form
 * isNotBackAjax: neu khong muon luu history url de back lai thi set la true, khi do se ko luu lai url khi goi ajax va trang duoc goi se khong back lai dc
 * strAlert: string dung de confirm khi co su dung confirm, mac dinh se la "Are you sure?" 
 */
function ajaxSubmit(div,link,style,form,isNotBackAjax,strAlert) {
	
	//alert(link);
	link=link.replace(/^.*#/, '');
	link=link.replace(/\//g,'-');
	formObj=document.getElementById(form);
	
	if(strAlert=="" || isNaN(strAlert) )strAlert="Are you sure?";
	switch(style){
		case 'one':;
		case 'deleteone':
			if(confirm(strAlert)){
				if(isNotBackAjax){link=link.replace(/-/g,'/');ajax(div,link,form);}
				else 	$.history.load(link,div,form);
			}
			break;
		case 'multi':;
		case 'delete':
			
			for (var i = 0; i < formObj.elements.length; i++) {
				if ( formObj.elements[i].checked == true && formObj.elements[i].id=='cb'){
					break;
				}
			}
			if(i==formObj.elements.length){
				alert('No selected data !');
				break;
			}
			if(confirm(strAlert)){
				if(isNotBackAjax){link=link.replace(/-/g,'/');ajax(div,link,form);}
				else 	$.history.load(link,div,form);
			}
			break;
		case 'multi_noneConfirm':			
			for (var i = 0; i < formObj.elements.length; i++) {
				if ( formObj.elements[i].checked == true && formObj.elements[i].id=='cb'){
					break;
				}
			}
			if(i==formObj.elements.length){
				alert('No selected data !');
				break;
			}
			
		default:			
			if(isNotBackAjax){link=link.replace(/-/g,'/');ajax(div,link,form);}
			else $.history.load(link,div,form);
		
	}
	
	return false;	
}

function ajax(container,action,form){	
	deactivateEditors();
	action=BACKEND_URL+'/'+action+'?t='+$.now();
	startAjaxEffect();
	
	if(form){		
		$('#'+form).ajaxSubmit({target:'#'+container,type:'POST',url:action,success:function(){	
			endAjaxEffect();			
		}});		
	}
	else{	
		$('#' + container + '').load(action,null,function(){
			endAjaxEffect();
		});
	}
}

function ajaxNoRespond(action,form){	
	action=BACKEND_URL+'/'+action+'?t='+$.now();	
	if(form){		
		$('#'+form).ajaxSubmit({type:'POST',url:action});		
	}
	else{	
		$.ajax({url:action});
	}
}

/*form submit ajax*/
function submitForm(form,div){
	startAjaxEffect();
	$('#'+form).attr('action',BACKEND_URL+'/'+$('#'+form).attr('action'));		
	$('#'+form).ajaxSubmit({target:'#'+div,type:'POST',success:function(){	
		endAjaxEffect();			
	}});
}

function startAjaxEffect(){
	if(ajaxCount++==0){
		$('#loading').css('display','inline');
		container_width=$('#mainContent').height();	
		container_height=$('#mainContent').height();
		var position = $('#mainContent').position();
		
		$('div[id=cover]').height($(document).height());
	    $('div[id=cover]').width($(document).width());   
	    
	    
	    //$('#imgcover').css('marginLeft',(($('#mainContent').width()-$('#imgcover').width())/2+(position.left-10))+'px');
	    var imgcovertop=$(window).scrollTop()+($(window).height()-$('#imgcover').height())/2;
	    var imgcoverleft=($(window).width()-$('#imgcover').width())/2;
	    $('#imgcover').css({'top':imgcovertop,'left':imgcoverleft});
	}
}

function endAjaxEffect(){
	//decrease ajax count
	if(--ajaxCount==0)$('#loading').css('display','none');	
}

function showMessage(container,textmessage,error){
	var message=$("#"+container).find("#message:first");
	
	message.text(textmessage);
	message.width(message.parent().width()-10);
	if(error){		
		message.attr("class","messageError");
	}
	else{
		message.attr("class","messageMessage");
	}
	message.fadeIn();
}
function removeMessage(){	
	$("body").find("#message").fadeOut();	
}

function checkSubmitUserEditPass() {
	
		if(!document.getElementById('UserUsername').value){
			alert('username is require!');
			document.getElementById('UserUsername').focus();
		}
		else if(empty(document.getElementById('UserOldPassword').value)){
			alert('Old password is require!');
			document.getElementById('UserOldPassword').focus();
		}
		
		else if(document.getElementById('UserNewPassword').value!=document.getElementById('UserConfirmPassword').value){
			alert('New password and Confirm is not the same!');
			document.getElementById('UserNewPassword').focus();
		}
		else if(document.getElementById('UserNewPassword').value.length>1 && document.getElementById('UserNewPassword').value.length<5){
			alert('New password must greater than 5 character!');
			document.getElementById('UserNewPassword').focus();
		}
		else if(!_JS_emailCheck(document.getElementById('UserEmail'))){
			alert('Invalid Email!');
			document.getElementById('UserEmail').focus();
		}
		
		else{
			submitForm('adminForm','mainContent');
		}
	

}

